
import * as cc from "cc";
import { VMCenter } from "../../../framework/vm/VMCenter";
import { VMEntity } from "../../../framework/vm/VMEntity";
import { CellEquipItem } from "../items/CellEquipItem";
import { AddPointWindow } from "../windows/AddPointWindow";
import { RenameWindow } from "../windows/RenameWindow";
import { PageBase } from "./PageBase";
const { ccclass, } = cc._decorator;

/**
 * Predefined variables
 * Name = PagePlayer
 * DateTime = Sun Mar 20 2022 13:24:27 GMT+0800 (中国标准时间)
 * Author = 272493431
 * FileBasename = PagePlayer.ts
 * FileBasenameNoExtension = PagePlayer
 * URL = db://assets/scripts/scene/VMDemo/page/PagePlayer.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
@ccclass('PagePlayer')
export class PagePlayer extends PageBase implements IMVVM{
    _vmTag: string;
    _vmEntitys: VMEntity[];


    onCtor(node: cc.Node): void {
        super.onCtor(node);
    }

    onLoaded(): void {
        
        // VMCenter.add(this.data,this);
        let prop_player = this.find('prop::player');

        let LabelPlayerName = this.find(">LabelPlayerName");
     
        let LabelLevel = this.find(">LabelLevel");

        let ExpBar = this.find(">ExpBar");
        let LabelExp = this.find(">LabelExp");
        let HpBar = this.find(">HpBar");
        let LabelHp = this.find(">LabelHp");
        let MpBar = this.find(">MpBar");
        let LabelMp = this.find(">LabelMp");

        let LabelPropPoint = this.find(">LabelPropPoint");
        let LabelSkillPoint = this.find(">LabelSkillPoint");
      
        let ButtonAddSkill = this.find(">ButtonAddSkill");
        let ButtonChangeName = this.find(">ButtonChangeName");
        let ButtonAddProp = this.find(">ButtonAddProp");
        let equip = this.find(">equip");

        // 打开重命名
        ButtonChangeName.on("click",()=>{
            let renameWindow = new RenameWindow();
        });

        // 打开加点
        ButtonAddProp.on("click",()=>{
            let renameWindow = new AddPointWindow();
           
        });



        // 切换到技能界面
        VMCenter.ButtonClick(this,ButtonAddSkill,{watchPath: "game.menu.state",operation(opts){

            let skill = cc.find("Canvas/background/skill");
            let index = skill.parent.children.indexOf(skill);

            return index;
        }})


        VMCenter.Label(this,LabelPlayerName,{watchPath: "player.name"}); // 玩家名字
        VMCenter.Label(this,LabelLevel,{watchPath: "player.level"}); // 等级
        VMCenter.Label(this,LabelPropPoint,{watchPath: "player.prop_point"}); // 属性点
        VMCenter.Label(this,LabelSkillPoint,{watchPath: "player.skill_point"}); // 技能点
        

        // 文本显示
        VMCenter.Label(this,LabelHp,{watchPath: ["player.hp","player.mhp"]}); // 血量
        VMCenter.Label(this,LabelMp,{watchPath: ["player.mp","player.mmp"]}); // 法力值
        VMCenter.Label(this,LabelExp,{watchPath: ["player.exp","player.exp_max"]}); //经验值

        // 进度条
        VMCenter.Progress(this,HpBar,{watchPath: ["player.hp","player.mhp"]});
        VMCenter.Progress(this,MpBar,{watchPath: ["player.mp","player.mmp"]});
        VMCenter.Progress(this,ExpBar,{watchPath: ["player.exp","player.exp_max"]});


        // 属性值
        prop_player.children.forEach((node)=>{
            VMCenter.Label(this,node,{watchPath: `player.${node.name}`}); //属性
        });

        // // 装备
        // VMCenter.Children(this,equip,{watchPath: "",formator(opts){

        // }});

        equip.children.forEach((node,index)=>{
            let cell = new CellEquipItem();
            cell.onCtor(node);
            VMCenter.Event(this,node,{watchPath: `player.equips.${index}`,onChangeEvent(opts){
                cell.index = opts.newValue;
            }});
        });
    }

    onUnloaded(): void {
          VMCenter.remove(this);
    }

}
